#!/usr/bin/python

from flask import Flask, jsonify, request
from src.rosemary import Rosemary
from src.args import parse_arguments

import json

app = Flask(__name__)
rosemary = Rosemary()
parse_arguments(rosemary)
rosemary.init_plugins()

@app.route('/api/v1.0/getPlugins', methods=['GET'])
def get_plugins():
    return jsonify(rosemary.get_plugins())

@app.route('/api/v1.0/getPluginItems/<int:id>', methods=['GET'])
def get_plugin_items(id):
    params = request.args.get('params')
    if not params is None:
        params = json.loads(params)
    return jsonify(rosemary.get_plugin_items(id, params))

@app.route('/api/v1.0/insertVideoToHistory', methods=['POST'])
def insert_video_to_history():
    video = request.json
    if video is None: return {}
    return jsonify(rosemary.insert_video_to_history(video))

@app.route('/api/v1.0/saveVideoToStorage', methods=['POST'])
def download_video():
    video = request.json
    if video is None: return {}
    return jsonify(rosemary.download_video(video))

if __name__ == '__main__':
    app.run(debug=True, host="0.0.0.0", port=int(rosemary.config.get_std_option('port')), use_reloader=False)


